package org.sagitta.httprpc.pox.adaptor;

import javax.xml.bind.JAXBContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sagitta.httprpc.AdaptorFactory;
import org.sagitta.httprpc.RequestUnmarshaller;
import org.sagitta.httprpc.ResponseMashaller;

public class POXAdaptorFactory extends AdaptorFactory {
	private static final Log log = LogFactory.getLog(POXAdaptorFactory.class);
	private static final String BASEMESSAGEPACKAGE = "org.sagitta.httprpc.pox.message";
	private JAXBContext context;
	private String messagePackage;

	@Override
	public RequestUnmarshaller createUnmashaller() throws Exception {
		POXRequestUnmashaller u = new POXRequestUnmashaller(context);
		log.info("Create an unmashaller to process POX request.");
		return u;
	}

	@Override
	public ResponseMashaller createMashaller() throws Exception {
		POXResponseMashaller m = new POXResponseMashaller(context);
		log.info("Create a mashaller to process POX response.");
		return m;
	}

	public String getMessagePackage() {
		return messagePackage;
	}

	public void setMessagePackage(String messagePackage) {
		this.messagePackage = messagePackage;
	}

	@Override
	public void initializeFactory() throws Exception {
		log.info("Use pox adaptor to mashaller/unmashaller the message.");
		context = JAXBContext.newInstance(BASEMESSAGEPACKAGE + messagePackage);
		log.info("Initialize successfully JAXB on package: "
				+ BASEMESSAGEPACKAGE + messagePackage);
	}

}
